package com.srwl.mytx.tao.av;


import android.content.Context;
import android.content.res.AssetManager;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.StatFs;
import android.text.TextUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileUtils {
    public static boolean isDebug = false;
    static final String TAG = "FileUtils";
    private static final int AVAIABLE_SPACE_SIZE = 50;

    public FileUtils() {
    }

    public static boolean copyAssets(Context pContext, String pAssetFilePath, String pDestDirPath) {
        AssetManager assetManager = pContext.getAssets();
        File temp = new File(pDestDirPath);
        if (!temp.exists()) {
            boolean success = temp.mkdirs();
            if (!success) {
                return false;
            }
        }

        InputStream in = null;
        FileOutputStream out = null;

        try {
            in = assetManager.open(pAssetFilePath);
            String fileName = pAssetFilePath;
            if (pAssetFilePath.indexOf("/") != -1) {
                int index = pAssetFilePath.lastIndexOf("/");
                fileName = pAssetFilePath.substring(index + 1);
            }

            File outFile = new File(pDestDirPath, fileName);
            out = new FileOutputStream(outFile);
            copyStream(in, out);
            out.flush();
            boolean var9 = true;
            return var9;
        } catch (IOException var23) {
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException var22) {
                }
            }

            if (out != null) {
                try {
                    out.close();
                } catch (IOException var21) {
                }
            }

        }

        return false;
    }

    public static boolean copyDirectiory(Context context, String sourceDir, String targetDir) {
        File temp = new File(targetDir);
        if (!temp.exists()) {
            boolean success = temp.mkdirs();
            if (!success) {
                return false;
            }
        }

        AssetManager assetManager = context.getAssets();
        InputStream in = null;
        FileOutputStream out = null;

        try {
            String[] fileStr = assetManager.list(sourceDir);

            for(int i = 0; i < fileStr.length; ++i) {
                File outFile = new File(targetDir, fileStr[i]);
                if (!outFile.exists()) {
                    in = assetManager.open(sourceDir + "/" + fileStr[i]);
                    out = new FileOutputStream(outFile);
                    copyStream(in, out);
                    out.flush();
                }
            }

            boolean var25 = true;
            return var25;
        } catch (IOException var22) {
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException var21) {
                }
            }

            if (out != null) {
                try {
                    out.close();
                } catch (IOException var20) {
                }
            }

        }

        return false;
    }

    public static boolean deleteFile(String filePath) {
        if (filePath == null) {
            return false;
        } else {
            File file = new File(filePath);
            return file.exists() && file.isFile() ? file.delete() : true;
        }
    }

    private static void copyStream(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[16384];

        int read;
        while((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }

    }

    public static boolean copyFile(String srcFileName, String desFileName) {
        InputStream in = null;
        FileOutputStream out = null;

        try {
            File temp = new File(srcFileName);
            in = new FileInputStream(temp);
            File outFile = new File(desFileName);
            File parFile = outFile.getParentFile();
            if (parFile != null && !parFile.exists()) {
                parFile.mkdirs();
            }

            out = new FileOutputStream(outFile);
            copyStream(in, out);
            out.flush();
            boolean var7 = true;
            return var7;
        } catch (IOException var21) {
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException var20) {
                }
            }

            if (out != null) {
                try {
                    out.close();
                } catch (IOException var19) {
                }
            }

        }

        return false;
    }

    public static void clearTempFiles(String videoPath) {
        if (!isDebug) {
            File dir = new File(videoPath);
            if (dir.exists() && dir.isDirectory()) {
                File[] files = dir.listFiles();
                File[] arr$ = files;
                int len$ = files.length;

                for(int i$ = 0; i$ < len$; ++i$) {
                    File file = arr$[i$];
                    if (!file.isDirectory()) {
                        String filename = file.getName();
                        if (filename.endsWith(".mp4") && filename.startsWith("temp_")) {
                            file.delete();
                        }
                    }
                }
            }

        }
    }

    public static String getVideoVid(String name) {
        return TextUtils.isEmpty(name) ? null : name.substring(name.lastIndexOf("/") + 1, name.lastIndexOf("."));
    }

    public static String getVideoDir(String name) {
        return TextUtils.isEmpty(name) ? null : name.substring(0, name.lastIndexOf("/") + 1);
    }

    public static boolean checkExtenExternalStorageMounted() {
        String state = Environment.getExternalStorageState();
        return "mounted".equals(state);
    }

    public static void asyncBatchDelete(String... list) {
        File[] file_list = new File[list.length];

        for(int i = 0; i < file_list.length; ++i) {
            file_list[i] = new File(list[i]);
        }

        asyncBatchDelete(file_list);
    }

    public static void asyncBatchDelete(File... list) {
        (new AsyncTask<File, Void, Void>() {
            protected Void doInBackground(File... params) {
                File[] arr$ = params;
                int len$ = params.length;

                for(int i$ = 0; i$ < len$; ++i$) {
                    File f = arr$[i$];
                    f.delete();
                }

                return null;
            }
        }).execute(list);
    }

    public static void isDirExist(String path) {
        if (!TextUtils.isEmpty(path)) {
            File outPutFile = new File(path);
            if (outPutFile.exists() && outPutFile.isDirectory()) {
                return;
            }

            outPutFile.mkdir();
        }

    }

    public static boolean isAvaiableSpace() {
        boolean ishasSpace = false;
        if (Environment.getExternalStorageState().equals("mounted")) {
            String sdcard = Environment.getExternalStorageDirectory().getPath();
            StatFs statFs = new StatFs(sdcard);
            long blockSize = (long)statFs.getBlockSize();
            long avaiableCount = (long)statFs.getAvailableBlocks();
            long availableSpare = avaiableCount * blockSize / 1048576L;
            if (availableSpare > 50L) {
                ishasSpace = true;
            }
        }

        return ishasSpace;
    }

    public static String getDefaultFileDir(Context context) {
        String dir;
        if (context.getExternalCacheDir() == null) {
            dir = context.getCacheDir().getAbsolutePath() + File.separator + "taorecorder_video";
        } else {
            dir = context.getExternalCacheDir().getAbsolutePath() + File.separator + "taorecorder_video";
        }

        isDirExist(dir);
        return dir;
    }

    public static class ExtensionFilter implements FilenameFilter {
        public final String _DotExtension;

        public ExtensionFilter(String ext) {
            this._DotExtension = "." + ext;
        }

        public boolean accept(File dir, String filename) {
            return filename.endsWith(this._DotExtension);
        }
    }
}

